package controllers

import (
	beego "github.com/beego/beego/v2/server/web"
	"ware/models"
)

type WareController struct {
	beego.Controller
}
type JsonRes struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func (c *WareController) Add() {
	name := c.GetString("name")
	address := c.GetString("address")
	types := c.GetString("types")
	if name == "" || address == "" {
		c.Data["json"] = JsonRes{
			Code: 500,
			Msg:  "仓库名称，地址是必填项",
			Data: nil,
		}
		c.ServeJSON()
	}
	ware := models.Ware{
		Name:    name,
		Address: address,
		Types:   types,
	}
	err := models.DB.Create(&ware).Error
	if err != nil {
		c.Data["json"] = JsonRes{
			Code: 500,
			Msg:  "添加失败",
			Data: nil,
		}
		c.ServeJSON()
	} else {
		c.Data["json"] = JsonRes{
			Code: 200,
			Msg:  "添加成功",
			Data: nil,
		}
		c.ServeJSON()
	}
}
